
/*
 * .
 * All Rights Reserved.
 */
package com.model.demo.util;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.fit.pdfdom.PDFDomTree;

import java.io.*;

/**
 * PdfConvertUtil
 *
 * @author Liao Dongshan, 2019年05月08日
 *
 */
public class PdfConvertUtil {
    public void pdftohtml(InputStream in, String htmlPath) throws Exception {
        //加载PDF文档
        PDDocument document = PDDocument.load(in);
        //将字节流转换成字符流
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(htmlPath)),"UTF-8"));
        //实例化pdfdom树对象
        PDFDomTree pdfDomTree = new PDFDomTree();
        //开始写入html文件
        pdfDomTree.writeText(document, out);
        //在文件末尾写入要引入的js，因为我将转换的html文件放在了webapp/pdfhtml文件夹下，所以这两个js文件也要放在pdfhtml文件夹下
        out.write("<script type=\"text/javascript\" src=\"jquery-3.2.1.js\"></script>\n" +
                "<script type=\"text/javascript\" src=\"demo.js\"></script>");
        out.flush();
        out.close();
        document.close();
    }

}
